define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Starbusiness_pushinfo_apply, $stateParams, GoAddress, GoCatTypeAuth, GoDialog,GoPage) {

		//数据模型
		$scope.input = new Object();
		$scope.input.id = $stateParams.id;
		$scope.input.province = null; //省市区三级联动
		$scope.input.city = null;
		$scope.input.district = null;
		$scope.input.title = "";
		$scope.input.content = "";
		$scope.method = new Array();//要提交的
		$scope.store = new Array();//要提交的
		$scope.region_id = "";
		$scope.region_name = "";
		$scope.cat_id = "";
		$scope.cat_name = "";

		$scope.cats = new Array(); //显示的类目
		$scope.regions = new Array(); //显示的地区
		$scope.input.cat_type = new Array(); //提交的结果{id:"001"}
		$scope.input.region = new Array(); //提交的结果{id:"001"}

		$scope.input.method = [false,false];
		$scope.input.stores = [false,false,false,false];

		$scope.initialize = function() { //后面要初始化这3个参数，所以把他们放在函数里
			$scope.input.cat1 = null;
			$scope.input.cat2 = null;
			$scope.input.cat3 = null;
			$scope.input.province = null;
			$scope.input.city = null;
			$scope.input.district = null;
		}
		$scope.initialize();

		//初始化数据
		$scope.init_data = function() { //根据id查找
			Starbusiness_pushinfo_apply.get_massage($scope.input.id, function(data) {
				console.log(data);
				if(data) {
					$scope.input.title = data.title;
					var reg=/<[^<>]+>/g;//过滤html
        			$scope.input.content = data.content.replace(reg,'');
					$scope.method = data.method;
					$scope.store = data.store;
					$scope.bind_method();
					$scope.bind_store();
					//绑定地区显示
					for(var i = 0; i<data.region.length;i++){
						$scope.region_name = "";
						$scope.region_id = "";
						var r = data.region[i];
						$scope.region_name = r.substr(0,r.indexOf(":"));
						$scope.region_id = r.substr(r.indexOf(":")+1);
						$scope.regions.push($scope.region_name);
						$scope.input.region.push($scope.region_id);
					}
					//绑定品类显示
					for(var j=0; j<data.cat_type.length;j++){
						$scope.cat_id = "";
						$scope.cat_name = "";
						var c = data.cat_type[j];
						$scope.cat_name = c.substr(0,c.indexOf(":"));
						$scope.cat_id = c.substr(c.indexOf(":")+1);
						$scope.cats.push($scope.cat_name);
						$scope.input.cat_type.push($scope.cat_id);
					}
				}
			});
		}
		$scope.init_data();
		
		//绑定推送对象
		$scope.bind_method = function() {
			for(var k = 0; k < $scope.method.length; k++) {
				$scope.input.method[$scope.method[k] - 1] = true;
			}
		}
		//绑定推送方式
		$scope.bind_store = function() {
			for(var k = 0; k < $scope.store.length; k++) {
				$scope.input.stores[$scope.store[k] - 1] = true;
			}
		}

		//下拉侦听，目前只能省市区三级联动
		$scope.listen_province = function(data) {
			$scope.input.city = null;
			$scope.input.district = null;
		};
		$scope.listen_city = function(data) {
			$scope.input.district = null;
		};
		//定义绑定
		var items = new Array();
		var obj1 = {
			name: 'input.province'
		};
		var obj2 = {
			name: 'input.city'
		};
		var obj3 = {
			name: 'input.district'
		};
		items.push(obj1);
		items.push(obj2);
		items.push(obj3);
		GoAddress.init_load($scope, items);
		$scope.doSelectProvince = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择省份",$scope.province_items,$scope.input.province,function(data){
				$scope.input.province = data;
			});
		}
		$scope.doSelectCity = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择城市",$scope.city_items,$scope.input.city,function(data){
				$scope.input.city = data;
			});
		}
		$scope.doSelectDistrict = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择区/县",$scope.district_items,$scope.input.district,function(data){
				$scope.input.district = data;
			});
		}
		$scope.doSelectCat1 = function(){
			//id是值 name是显示
			GoDialog.showSelect("大分类",$scope.cat1_items,$scope.input.cat1,function(data){
				$scope.input.cat1 = data;
			});
		}
		$scope.doSelectCat2 = function(){
			//id是值 name是显示
			GoDialog.showSelect("中分类",$scope.cat2_items,$scope.input.cat2,function(data){
				$scope.input.cat2 = data;
			});
		}
		$scope.doSelectCat3 = function(){
			//id是值 name是显示
			GoDialog.showSelect("小分类",$scope.cat3_items,$scope.input.cat3,function(data){
				$scope.input.cat3 = data;
			});
		}
		/***分类三级联动***/
		$scope.input.brand = null; //这是品牌下拉框的绑定数据
		$scope.input.cat1 = null;
		$scope.input.cat2 = null;
		$scope.input.cat3 = null;
		//下拉侦听
		$scope.listen_brand = function(data, obj) {
			$scope.input.cat1 = null;
			$scope.input.cat2 = null;
			$scope.input.cat3 = null;
		};
		$scope.listen_cat1 = function(data, obj) {
			$scope.input.cat2 = null;
			$scope.input.cat3 = null;
		};
		$scope.listen_cat2 = function(data, obj) {
			$scope.input.cat3 = null;
		};
		//定义绑定
		var items_cat = new Array();
		var obj_brand = {
			name: 'input.brand'
		};
		var obj1_cat = {
			name: 'input.cat1'
		};
		var obj2_cat = {
			name: 'input.cat2'
		};
		var obj3_cat = {
			name: 'input.cat3'
		};
		items_cat.push(obj_brand);
		items_cat.push(obj1_cat);
		items_cat.push(obj2_cat);
		items_cat.push(obj3_cat);
		GoCatTypeAuth.init_load_only($scope, items_cat);

		//地域添加
		$scope.add_region = function() {
			$scope.region_id = "";
			$scope.region_name = "";
			if($scope.input.district) {
				$scope.region_name = $scope.input.province.name + "/" + $scope.input.city.name + "/" + $scope.input.district.name;
				$scope.region_id = $scope.region_name + ":" + $scope.input.district.id;
			} else if($scope.input.city) {
				$scope.region_name = $scope.input.province.name + "/" + $scope.input.city.name;
				$scope.region_id = $scope.region_name + ":" + $scope.input.city.id;
			} else if($scope.input.province) {
				$scope.region_name = $scope.input.province.name;
				$scope.region_id = $scope.region_name + ":" + $scope.input.province.id;
			}
			if($scope.region_id && $scope.region_name) {
				if($scope.check($scope.region_name, 2)) {
					$scope.regions.push($scope.region_name);
					$scope.input.region.push($scope.region_id);
					$scope.initialize(); //初始化分类3个modal
				} else {
					GoDialog.toast("已存在该地区！");
				}
			}
		}

		//判断存在性
		$scope.check = function(names, type) {
			if(type == 1) {
				for(var i = 0; i < $scope.cats.length; i++) {
					if($scope.cats[i] == names) {
						return false;
					}
				}
			} else {
				for(var i = 0; i < $scope.regions.length; i++) {
					if($scope.regions[i] == names) {
						return false;
					}
				}
			}
			return true;
		}

		//分类添加
		$scope.add_act_type = function() {
			$scope.cat_id = "";
			$scope.cat_name = "";
			if($scope.input.cat3) {
				$scope.cat_name = $scope.input.cat1.name + "/" + $scope.input.cat2.name + "/" + $scope.input.cat3.name;
				$scope.cat_id = $scope.cat_name + ":" + $scope.input.cat3.id;
			} else if($scope.input.cat2) {
				$scope.cat_name = $scope.input.cat1.name + "/" + $scope.input.cat2.name;
				$scope.cat_id = $scope.cat_name + ":" + $scope.input.cat2.id;
			} else if($scope.input.cat1) {
				$scope.cat_name = $scope.input.cat1.name;
				$scope.cat_id = $scope.cat_name + ":" + $scope.input.cat1.id;
			}
			if($scope.cat_id && $scope.cat_name) {
				if($scope.check($scope.cat_name, 1)) {
					$scope.cats.push($scope.cat_name);
					$scope.input.cat_type.push($scope.cat_id);
					$scope.initialize(); //初始化分类3个modal
				} else {
					GoDialog.toast("已存在该品类！");
				}
			}
		}

		//删除地域
		$scope.delete_region = function(val) {
				$scope.regions.splice(val, 1); //在val下标删除一个元素，后面的元素会自己填充这个下标
				$scope.input.region.splice(val, 1);
			}
			//删除类目
		$scope.delete_act_type = function(val) {
			$scope.cats.splice(val, 1); //在val下标删除一个元素，后面的元素会自己填充这个下标
			$scope.input.cat_type.splice(val, 1);
		}

		//选择推送对象和方式
		$scope.select_id = function() {
			//推送对象
			$scope.store.splice(0,$scope.store.length);//一定要先清空数组，否则数据重复
			for(var k = 0;k<$scope.input.stores.length;k++) {
				if($scope.input.stores[k]) {
					$scope.store.push(parseInt(k) + 1);
				}
			}
			//推送方式
			$scope.method.splice(0,$scope.method.length);//一定要先清空数组，否则数据重复
			for(var k = 0;k<$scope.input.method.length;k++) {
				if($scope.input.method[k]) {
					$scope.method.push(parseInt(k) + 1);
				}
			}
		}

		//提交
		$scope.save = function() {
			$scope.select_id();//获取推送对象和方式
			if($scope.input.title == "") {
				GoDialog.toast("请输入标题！");
			} else if($scope.input.region.length == 0) {
				GoDialog.toast("请选择地域！");
			} else if($scope.input.cat_type.length == 0) {
				GoDialog.toast("请选择分类！");
			} else {
				Starbusiness_pushinfo_apply.get_add($scope.input.id,
					$scope.input.title,
					$scope.input.content,
					$scope.method,
					$scope.input.region,
					$scope.input.cat_type,
					$scope.store,
					function(data) {
						if(data.success) {
							GoDialog.toast("提交成功！");
							GoPage.go_states("starbusiness_pushinfo");
						} else {
							GoDialog.toast(data.msg);
						}
					});
			}
		}
		
		

	}

	controller.$inject = ['$scope', 'Starbusiness_pushinfo_apply', '$stateParams', 'GoAddress', 'GoCatTypeAuth', 'GoDialog','GoPage'];

	return app.registerController("starbusiness_pushinfo_applyctrl",controller);
});